package org.rency.mq.activemq.jms.factory;

import org.rency.mq.activemq.domain.DestinationInfo;
import org.rency.mq.activemq.service.DestinationInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class DestinationInfoFactory {

    private Logger logger = LoggerFactory.getLogger(DestinationInfoFactory.class);
    private Map<Integer, DestinationInfo> cachedDestinationInfo = new HashMap<Integer, DestinationInfo>(100);
    private DestinationInfoService destinationInfoService;

    public DestinationInfo getDestinationInfo(Integer actionId) {
        DestinationInfo destinationInfo = cachedDestinationInfo.get(actionId);

        if (destinationInfo == null) {
            try {
                destinationInfo = getDestinationInfoService().findByActionId(actionId);
            } catch (Exception e) {
                logger.warn("Cannot find destination from actionId: " + actionId);
            }
            if (destinationInfo == null) {
                return null;
            }
            cachedDestinationInfo.put(actionId, destinationInfo);
        }
        return destinationInfo;
    }

    public void setDestinationInfoService(DestinationInfoService destinationInfoService) {
        this.destinationInfoService = destinationInfoService;
    }

    public DestinationInfoService getDestinationInfoService() {
        return destinationInfoService;
    }
}
